'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const d = require('../nodes/Alias.cjs'),
  g = require('./compose-collection.cjs'),
  f = require('./compose-scalar.cjs'),
  b = require('./resolve-end.cjs'),
  y = require('./util-empty-scalar-position.cjs'),
  S = { composeNode: A, composeEmptyNode: p };
function A(t, e, i, a) {
  const { spaceBefore: l, comment: o, anchor: s, tag: n } = i;
  let c,
    u = !0;
  switch (e.type) {
    case 'alias':
      (c = h(t, e, a)),
        (s || n) &&
          a(e, 'ALIAS_PROPS', 'An alias node must not specify any properties');
      break;
    case 'scalar':
    case 'single-quoted-scalar':
    case 'double-quoted-scalar':
    case 'block-scalar':
      (c = f.composeScalar(t, e, n, a)),
        s && (c.anchor = s.source.substring(1));
      break;
    case 'block-map':
    case 'block-seq':
    case 'flow-collection':
      (c = g.composeCollection(S, t, e, n, a)),
        s && (c.anchor = s.source.substring(1));
      break;
    default: {
      const m =
        e.type === 'error' ? e.message : `Unsupported token (type: ${e.type})`;
      a(e, 'UNEXPECTED_TOKEN', m),
        (c = p(t, e.offset, void 0, null, i, a)),
        (u = !1);
    }
  }
  return (
    s &&
      c.anchor === '' &&
      a(s, 'BAD_ALIAS', 'Anchor cannot be an empty string'),
    l && (c.spaceBefore = !0),
    o &&
      (e.type === 'scalar' && e.source === ''
        ? (c.comment = o)
        : (c.commentBefore = o)),
    t.options.keepSourceTokens && u && (c.srcToken = e),
    c
  );
}
function p(
  t,
  e,
  i,
  a,
  { spaceBefore: l, comment: o, anchor: s, tag: n, end: c },
  u,
) {
  const m = {
      type: 'scalar',
      offset: y.emptyScalarPosition(e, i, a),
      indent: -1,
      source: '',
    },
    r = f.composeScalar(t, m, n, u);
  return (
    s &&
      ((r.anchor = s.source.substring(1)),
      r.anchor === '' && u(s, 'BAD_ALIAS', 'Anchor cannot be an empty string')),
    l && (r.spaceBefore = !0),
    o && ((r.comment = o), (r.range[2] = c)),
    r
  );
}
function h({ options: t }, { offset: e, source: i, end: a }, l) {
  const o = new d.Alias(i.substring(1));
  o.source === '' && l(e, 'BAD_ALIAS', 'Alias cannot be an empty string'),
    o.source.endsWith(':') &&
      l(e + i.length - 1, 'BAD_ALIAS', 'Alias ending in : is ambiguous', !0);
  const s = e + i.length,
    n = b.resolveEnd(a, s, t.strict, l);
  return (o.range = [e, s, n.offset]), n.comment && (o.comment = n.comment), o;
}
exports.composeEmptyNode = p;
exports.composeNode = A;
